package org.jflame.commons.model;

import org.jflame.commons.exception.BusinessException;

public class SimpleResult implements BaseResult {

    private static final long serialVersionUID = 1L;
    private int status;
    private String message;

    public SimpleResult() {
    }

    public SimpleResult(int status, String message) {
        this.status = status;
        this.message = message;
    }

    @Override
    public int getStatus() {
        return status;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public SimpleResult status(int status) {
        this.status = status;
        return this;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setMessage(String format, Object... args) {
        this.message = String.format(format, args);
    }

    public SimpleResult message(String message) {
        this.message = message;
        return this;
    }

    public void setResult(BaseResult result) {
        status = result.getStatus();
        message = result.getMessage();
    }

    public void setResult(int status, String message) {
        this.status = status;
        this.message = message;
    }

    /**
     * 使用错误编码和描述生成一个通用业务异常
     * 
     * @return
     */
    public BusinessException toExcept() {
        return new BusinessException(this);
    }

    @SuppressWarnings("unchecked")
    public <T> CallResult<T> createCallResult() {
        if (this instanceof CallResult) {
            return (CallResult<T>) this;
        }

        return new CallResult<T>(getStatus(), getMessage());
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("status=")
                .append(status)
                .append(", ");
        if (message != null) {
            builder.append("message=")
                    .append(message);
        }
        return builder.toString();
    }

}
